﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CY.Utility.Common
{
    using System.Net;
    using System.Net.Mail;

    public class EmailUtility
    {
        /// <summary>
        /// 使用Gmail发送邮件
        /// </summary>
        /// <param name="gmailAccount">Gmail账号</param>
        /// <param name="gmailPassword">Gmail密码</param>
        /// <param name="to">接收人邮件地址</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="attachPath">附件</param>
        /// <returns>是否发送成功</returns>
        public static bool SendEmailThroughGmail(String gmailAccount, String gmailPassword, String to, String subject, String body,
            String attachPath)
        {
            return EmailUtility.TrySendEmail(
                "smtp.gmail.com",
                587,
                true,
                to,
                gmailAccount,
                gmailAccount,
                gmailPassword,
                false,
                subject,
                body,
                attachPath);
            /*
            try
            {
                SmtpClient client = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(gmailAccount, gmailPassword),
                    Timeout = 20000
                };

                using (MailMessage mail = new MailMessage(gmailAccount, to) { Subject = subject, Body = body })
                {
                    mail.IsBodyHtml = true;

                    if (!string.IsNullOrEmpty(attachPath))
                    {
                        mail.Attachments.Add(new Attachment(attachPath));
                    }

                    client.Send(mail);
                }
            }
            catch
            {
                return false;
            }

            return true;*/
        }

        public static bool TrySendEmail(
            string host,
            int port,
            bool enableSsl,
            string to,
            string from,
            string loginName,
            string password,
            bool useDefaultCredentials,
            string subject,
            string body,
            string attachPath)
        {
            try
            {
                SmtpClient client = new SmtpClient();
                client.Host = host;
                client.Port = port;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = enableSsl;
                client.UseDefaultCredentials = useDefaultCredentials;
                if (!useDefaultCredentials)
                {
                    client.Credentials = new NetworkCredential(loginName, password);
                }
                client.Timeout = 20000;

                using (MailMessage mail = new MailMessage(from, to) { Subject = subject, Body = body, IsBodyHtml = true })
                {
                    if (!string.IsNullOrEmpty(attachPath))
                    {
                        mail.Attachments.Add(new Attachment(attachPath));
                    }

                    client.Send(mail);
                }
            }
            catch
            {
                return false;
            }

            return true;
        }
    }
}
